/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.ugent.medirem.persistence.entity;

import be.ugent.medirem.persistence.util.Base64;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;

/**
 *
 * @author Thomas Roelens <thomas.roelens@ugent.be>
 */
@Entity
public class Account implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(unique = true, nullable = false)
    private String username;
    /**
     * The password is encrypted with SHA-1
     */
    @Column(nullable = false)
    private String password;
    @Column(nullable = false)
    private String rolename;
    private String firstname;
    private String surname;
    @OneToOne
    private Location location;

    public Account() {
    }

    public Account(String username, String password, String rolename, String firstname, String surname) {
        this.username = username;
        this.password = encryptPwd(password);
        this.rolename = rolename;
        this.firstname = firstname;
        this.surname = surname;
    }

    public Account(String username, String password, String rolename, String firstname, String surname, Location location) {
        this(username, password, rolename, firstname, surname);
        this.location = location;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Location getLocation() {
        return location;
    }

    public void setLocation(Location location) {
        this.location = location;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = encryptPwd(password);
    }

    public String getRolename() {
        return rolename;
    }

    public void setRolename(String rolename) {
        this.rolename = rolename;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    /**
     * Encrypts a password.
     *
     * @param  pwd     the password that must be encrypted.
     * @return         the encrypted password.
     */
    public static String encryptPwd(String pwd) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            md.update(pwd.getBytes("UTF-8"));
            byte[] raw = md.digest();

            return Base64.encodeBytes(raw);

        } catch (NoSuchAlgorithmException e) {
            System.err.println("Hashing algorithm could not be found!");
            System.err.println(e.getMessage());
        } catch (UnsupportedEncodingException e) {
            System.err.println("Hashing algorithm could not be found!");
            System.err.println(e.getMessage());
        }
        return null;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Account)) {
            return false;
        }
        Account other = (Account) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "be.ugent.medirem.persistence.entity.Account[id=" + id + "]";
    }
}
